package com.ht.medicaltreatment.view

import android.content.Context
import android.graphics.drawable.BitmapDrawable
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.PopupWindow
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.ToastUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.adapter.ItemAdapter
import com.ht.medicaltreatment.api.RetrofitUtils
import com.ht.medicaltreatment.bean.IceboxItemListBean
import com.ht.medicaltreatment.bean.ProjectBean
import com.ht.medicaltreatment.utils.backgroundAlpha
import com.xgst.driverlesscars.api.ApiService
import com.xgst.lib.http.ApiResult
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch

/**
 * 方案号  项目列表
 */
class ProjectDialog(context: Context,checkId:Int = -1):PopupWindow(context), OnItemClickListener {
    private val view:View
    private var adapter:ItemAdapter
    private var onClick:(Int,String)->Unit = {name,id ->}
    init {
        view =
            LayoutInflater.from(context).inflate(R.layout.dialog_item_list_layout, null, false)
        contentView = view
        isOutsideTouchable = true
        setBackgroundDrawable(BitmapDrawable())
        isTouchable = true
        width = WindowManager.LayoutParams.MATCH_PARENT
        height = ScreenUtils.getScreenHeight()/2
        animationStyle = R.style.itemPopStyle
        adapter = ItemAdapter(checkId)
        val recyclerView = view.findViewById<RecyclerView>(R.id.recyclerView)
        recyclerView.layoutManager = LinearLayoutManager(context)
        adapter.setOnItemClickListener(this)
        recyclerView.adapter = adapter

        initData()

        setOnDismissListener {
            ActivityUtils.getTopActivity().backgroundAlpha(1.0f)
        }
    }

    private fun initData() {
        projects({
            val list = mutableListOf<IceboxItemListBean>()
            it.forEach { bean->
                list.add(IceboxItemListBean(bean.id,bean.protocolNumber))
            }
           adapter.setNewInstance(list)
        })
    }

    fun show(onClick:(Int,String)->Unit) {
        if(isShowing){
            return
        }
        this.onClick = onClick
        ActivityUtils.getTopActivity().backgroundAlpha(0.4f)
        showAtLocation(view, Gravity.BOTTOM,0,0)
    }

    //项目
    private fun projects(onResult:(MutableList<ProjectBean>)->Unit){
        MainScope().launch {
            val bean = RetrofitUtils.initRetrofit(ApiService::class.java).projects()
            when(bean){
                is ApiResult.Success -> onResult(bean.data.data!!)
                is ApiResult.Error ->  {
                    dismiss()
                    ToastUtils.showShort(bean.errorMessage)
                }
            }
        }
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = adapter.getItem(position) as IceboxItemListBean
        if(item.disable.isEmpty()){
            onClick(item.id,item.name)
            dismiss()
        }
    }
}